package com.example.controller;

import com.example.pojo.Users;
import com.example.server.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("users")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/all")
    public List<Users> the(Model model, Users users){
       return userService.findAll(null);

        //执行查询方法


    }

    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        Users users=new Users();
        model.addAttribute("users",users);
        System.out.println("执行跳转到添加页面");
        return "add";//跳转页面
    }


    @RequestMapping("/add")
    public String add(Model model, Users users){
        System.out.println("执行查询方法");
        userService.add(users);
        System.out.println("执行重新查询");
        return "redirect:/all";
    }


    @RequestMapping("findById")
    public String findById(Model model,String id){
        Users users=userService.findById(id);
        System.out.println("执行查询id");
        model.addAttribute("users",users);
        return "update";

    }


    @RequestMapping("/update")
    public String update(Model model, Users users){
        System.out.println("执行修改方法");
        userService.update(users);
        System.out.println("执行重新查询");
        return "redirect:/all";
    }

    @RequestMapping("/del")
    public String del(String id){
        System.out.println("执行删除方法");
        userService.del(id);
        return "redirect:/all";
    }


    @RequestMapping("findSelect")
    public String findSelect(String age){
        userService.findSelect(age);

        //执行搜索
        System.out.println("执行搜索");
        return "redirect:/all";
    }

//    @RequestMapping("page")
//    public String page(@RequestParam Map<String,String> map,Model model){
//        List<Users> users=userService.findAll(null);
//        if (map.size()==0 || map.get("currentPage").isEmpty()){
//            map.put("currentPage","1");
//        }
//        PageUtil currentPage=new PageUtil(Integer.valueOf(map.get("currentPage")),2,users);
//        model.addAttribute("users",currentPage.getList());
//        model.addAttribute("currentPage",currentPage);
//        return "users";
//    }



}
